package com.gang.mars.redisson.Thread;

import lombok.SneakyThrows;

/**
 * 死锁
 * */
public class Apple11LoopLock {

    public static void main(String[] args) {

        Makeup girl1 = new Makeup(0, "小红");
        Makeup girl2 = new Makeup(1, "小明");

        girl1.start();
        girl2.start();
    }


    static class Lipstick{

    }

    static class Mirror{

    }

    static class Makeup extends Thread{

        static Lipstick lipstick = new Lipstick();
        static Mirror mirror = new Mirror();

        int choice;
        String girl;

        public Makeup(int choice, String girl){
            this.choice = choice;
            this.girl = girl;
        }

        @SneakyThrows
        @Override
        public void run() {
            makeup0();
        }

        private void makeup0() throws InterruptedException {
            if (choice ==0){
                synchronized (lipstick){
                    System.out.println(this.girl + "获得口红锁");
                    Thread.sleep(1000);
                    synchronized (mirror){
                        System.out.println(this.girl +"获取镜子锁");
                    }
                }

            }else {

                synchronized (mirror){
                    System.out.println(this.girl +"获取镜子锁");
                    Thread.sleep(3000);

                    synchronized (lipstick){
                        System.out.println(this.girl + "获得口红锁");
                    }
                }
            }
        }
    }
}
